package com.uvlab.sitespot.service;

import com.uvlab.sitespot.common.JxQuote;
import com.uvlab.sitespot.init.JxAppBootScanner;
import com.uvlab.sitespot.service.data.JxAppDataService;
import com.uvlab.sitespot.service.fdb.JxAppFdbService;
import com.uvlab.sitespot.service.member.JxMemberService;
import com.uvlab.sitespot.service.sedt.JxSiteEditorService;
import com.uvlab.sitespot.service.settings.JxAppSettingService;
import com.uvlab.sitespot.service.task.JiTaskService;
import com.uvlab.sitespot.service.task.JxRemoteTaskService;
import com.uvlab.sitespot.service.user.JxUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * Application Service, which holds references to all other service objects.
 * The UI and I/O layer controller can access all services through JxAppService
 * only.
 *
 * Created by Zhang Wei on 4/20/2016.
 */
@Service("svc")
@Scope("singleton")
public class JxAppService implements InitializingBean,DisposableBean {

    private volatile static JxAppService singleton = null;
    private static final Logger log = LoggerFactory.getLogger(JxAppService.class);

    protected JxAppBootScanner scanner;
    protected String datadir;
    protected JxAppDataService data;
    protected JxAppSettingService settings;

    protected JxAppFdbService fdb = null;
//    protected JiTaskService task=null;
    protected JxRemoteTaskService tasks;
    protected JxMemberService mem;
    protected JxUserService uses;
    public JxAppService() {
        this.settings = new JxAppSettingService();
    }

    /**
     * Return the singleton instance of the JxAppService object.
     * If you use Spring Framework, you can also do this by using Spring.
     *
     * @return
     */
    public static JxAppService getInstance() {
        if (singleton == null) {
            synchronized (JxAppService.class) {
                if (singleton == null)
                    singleton = new JxAppService();
            }
        }
        return singleton;
    }

    public void init(JxAppBootScanner scanner) {
        System.out.println("JxAppService.init()...");
        this.scanner = scanner;
        if (scanner == null) {
            this.datadir = "/temp/";
        } else {
            this.datadir = scanner.getAppDataDir();
        }
        dump();
    }

    public void dump()
    {
        System.out.println("AppRootDir = " + this.scanner.getAppRootDir());
        System.out.println("AppDataDir = " + this.datadir );
        this.tasks=new JxRemoteTaskService();
        this.fdb=new JxAppFdbService();
        this.mem=new JxMemberService();
    }

    public JxAppBootScanner getAppBootScanner()
    {
        return this.scanner;
    }

    public String getAppDataDir()
    {
        return this.datadir;
    }

    public String getAppRootDir()
    {
        return this.scanner.getAppRootDir();
    }

    public JxAppSettingService newAppSettingsService() {
        return new JxAppSettingService();
    }

    public JxAppSettingService getAppSettingsService() {
        return this.settings;
    }

    public JxAppDataService newAppDataService() {
        return new JxAppDataService();
    }

    public JxSiteEditorService newAppSiteEditorService() {
        JxSiteEditorService svc = new JxSiteEditorService();
        svc.init(this.getAppDataDir());
        return svc;
    }


    public JxQuote getQuote() {
        RestTemplate restTemplate = new RestTemplate();
        JxQuote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", JxQuote.class);
        log.info(quote.toString());
        return quote;
    }


    @Override
    public void destroy() throws Exception {
        System.out.println("JxAppService.destroy()...");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("JxAppService.afterPropertiesSet()...");
    }

    protected JxAppFdbService newAppFdbService() {
        String url = "http://192.168.1.100:8080";
        JxAppFdbService fdb = new JxAppFdbService();
        try {
            fdb.init(url);
        }
        catch (Exception e){
            fdb = null;
        }
        return fdb;
    }

    public JxAppFdbService getAppFdbService()
    {
        if (this.fdb == null)
        {
            this.fdb = newAppFdbService();
        }
        return this.fdb;
    }

    protected  JxRemoteTaskService newTaskService(){

        // The URL should be configurable
        // TODO: replace with getPropertyService().getTaskServiceUrl()
        String url= "http://192.168.1.100:8084";
        JxRemoteTaskService tasks = new JxRemoteTaskService();
        try {
            tasks.init(url);
        }
        catch (Exception e){
            tasks = null;
        }
        return tasks;
    }

    public  JxRemoteTaskService getTaskService(){
        if(this.tasks==null)
        {
            this.tasks=newTaskService();
        }
        return this.tasks;
    }


    protected  JxMemberService newMemberService(){
        String url= "http://192.168.1.100:8080";
        JxMemberService mem=new JxMemberService();
        try {
            mem.init(url);
        }
        catch (Exception e){
            mem = null;
        }
        return mem;
    }

    public  JxMemberService getMemberService(){
        if(this.mem==null)
        {
            this.mem=newMemberService();
        }
        return this.mem;
    }

    protected JxUserService newUserService(){
        String url= "http://192.168.1.100:8082";
        JxUserService uses=new JxUserService();
        try {
            uses.init(url);
        }
        catch (Exception e){
            uses = null;
        }
        return uses;
    }

    public  JxUserService getUserService(){
        if(this.uses==null)
        {
            this.uses=newUserService();
        }
        return this.uses;
    }
}